clear
set more off

forvalues b=2004/2013 {
	use ndcnums_`b'
	sort ndcnum
	save, replace
	clear

	use ndcnum pay coins copay deduct svcdate year using /disk/aging/mktscan/nongeo/data/100pct/ccaed`b'
	sort ndcnum
	merge m:1 ndcnum using ndcnums_`b'

	keep if oxycodone==1
	drop if mgs_rx==.
	gen month=month(svcdate)
	drop if month==.
	gen double any_oxy_kg=oxycodone*mgs_rx/1000
	gen double name_brand_oxy_kg=oxycontin*mgs_rx/1000
	gen pay_oop = copay + coins + deduct
	keep year month pay pay_oop any_oxy_kg name_brand_oxy_kg oxycodone oxycontin
	sort year month
	collapse (sum) pay pay_oop oxycodone oxycontin any_oxy_kg name_brand_oxy_kg (mean) pay_mean=pay ///
	pay_oop_mean = pay_oop, by(year month)
	save oxy_totals_`b', replace
	list 
	desc
}

clear
use oxy_totals_2004

forvalues b=2005/2013 {
	append using oxy_totals_`b'
}

save oxy_totals_all, replace
sort year month
outsheet using oxy_totals_all_pay.csv, comma replace
list 
desc
save oxy_pay, replace


